<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */
class Attachment extends AppModel
{
/**
 * Defining the name of model
 *
 */
    var $name="Attachment";
/**
 * Defining the name of the table
 *
 */
    var $useTable="attachments";
	
	var $displayField = 'filename';

/**
 * Finding all the related files in meeting model defined by the committee_id
 * $param committee_id finds all the files in meeting model 
 */
    function meetingFiles($cond){
        $this->bindModel(
            array(
                'belongsTo'=>array(
                    'Meeting' => array(
                        'className' => 'Meeting',
                        'foreignKey' => 'foreign_key',
                        'conditions' => array('Attachment.model'=>'Meeting'),
                    )
                )
            )
        );
        return $this->find('all',array('conditions'=>array($cond)));
    }

/**
 * Finding all the related files in decision model defined by the $cond
 * $param $cond finds all the files in decision model 
 */
 
    function decisionFiles($cond){
        $this->bindModel(
            array(
                'belongsTo'=>array(
                    'Decision' => array(
                        'className' => 'Decision',
                        'foreignKey' => 'foreign_key',
                        'conditions' => array('Attachment.model'=>'Decision'),
                    )
                )
            )
        );
        $cond['Decision.deleted']=0;
        return $this->find('all',array('conditions'=>$cond));
    }

/**
 * Finding all the related files in groupstatus model defined by the decision_id
 * $param decision_id finds all the files in groupstatus model 
 */
    function groupstatusFiles($cond){
        $this->bindModel(
            array(
                'belongsTo'=>array(
                    'Groupstatus' => array(
                        'className' => 'Groupstatus',
                        'foreignKey' => 'foreign_key',
                        'conditions' => array('Attachment.model'=>'Groupstatus'),
                    )
                )
            )
        );
        return $this->find('all',array('conditions'=>$cond));
    }

/**
 * Finding all the related files in userstatus model defined by the decision_id
 * $param decision_id finds all the files in userstatus model 
 */
    function userstatusFiles($cond){
        $this->bindModel(
            array(
                'belongsTo'=>array(
                    'Userstatus' => array(
                        'className' => 'Userstatus',
                        'foreignKey' => 'foreign_key',
                        'conditions' => array('Attachment.model'=>'Userstatus'),
                        'fields' => '',
                        'order' => '')
                )
            )
        );
        return $this->find('all',array('conditions'=>$cond));
    }
	

//get all the attachment which belongs to the committee with conditions
    function CommitteeFileId($committee_id,$not){
        $cond_meeting['Meeting.committee_id']=$committee_id;
        $cond_meeting['Meeting.deleted']=0;
	    $files1=$this->meetingFiles($cond_meeting);
        $files2=$this->decisionFiles(array('Decision.committee_id'=>$committee_id,'not'=>array('Decision.id'=>$not)));
        if(count($files2)){
            $files1=isset($files1)?am($files1,$files2):$files2;
        }
        $files3=$this->userstatusFiles(array('not'=>array('Userstatus.decision_id'=>$not),'Userstatus.committee_id'=>$committee_id));
        if(count($files3)){
            $files1=isset($files1)?am($files1,$files3):$files3;
        }
        $files4=$this->groupstatusFiles(array('not'=>array('Groupstatus.decision_id'=>$not),'Groupstatus.committee_id'=>$committee_id));
        if(count($files4)){
            $files1=isset($files1)?am($files1,$files4):$files4;
        }
        return Set::extract($files1,'{n}.Attachment.id');
    }
    
    //get all the attachment id which belongs to the committee
    function CommitteeFile($committee_id,$not,$cond){
        $cond_meeting['Meeting.committee_id']=$committee_id;
        $cond_meeting['Meeting.deleted']=0;
	    $files1=$this->meetingFiles(array_merge($cond_meeting,$cond));
        $files2=$this->decisionFiles(array_merge($cond,array('Decision.committee_id'=>$committee_id,'not'=>array('Decision.id'=>$not))));
        if(count($files2)){
            $files1=isset($files1)?am($files1,$files2):$files2;
        }
        $files3=$this->userstatusFiles(array_merge($cond,array('not'=>array('Userstatus.decision_id'=>$not),'Userstatus.committee_id'=>$committee_id)));
        if(count($files3)){
            $files1=isset($files1)?am($files1,$files3):$files3;
        }
        $files4=$this->groupstatusFiles(array_merge($cond,array('not'=>array('Groupstatus.decision_id'=>$not),'Groupstatus.committee_id'=>$committee_id)));
        if(count($files4)){
            $files1=isset($files1)?am($files1,$files4):$files4;
        }
        return $files1;
    }
    
    
    //check whether the current user have the permission to view the file
    function viewPermission($att,$user_id){
        if($user_id==$att['Attachment']['uploader']){
            //if the user is uploader
            return 1;
        }elseif($att['Attachment']['private']==0){
            //find attachment commitee_id
            if(!empty($att[$att['Attachment']['model']]['committee_id'])){
                $committee_id=$att[$att['Attachment']['model']]['committee_id'];
            }else{
                App::import('Model',$att['Attachment']['model']);
                $this->{$att['Attachment']['model']}=& ClassRegistry::init($att['Attachment']['model']);
                $data=$this->{$att['Attachment']['model']}->find('first',array('conditions'=>array($att['Attachment']['model'].'.id'=>$att['Attachment']['foreign_key'])));
                $committee_id=$data[$att['Attachment']['model']]['committee_id'];
            }
            //check whether the current user is committee user
            App::import('Model','Membership');
            $this->Membership=& ClassRegistry::init('Membership');
            $member=$this->Membership->find('first',array('conditions'=>array('Membership.user_id'=>$user_id,'Membership.committee_id'=>$committee_id)));
            return (!empty($member));
        }else{
            //private attachment
            
            //find the attachment meeting id
            App::import('Model','Decision');
            $this->Decision=& ClassRegistry::init('Decision');
            
            if($att['Attachment']['model']=='Decision'){
                //decision file
                if(!empty($att['Decision']['meeting_id'])){
                    $meeting_id=$att['Decision']['meeting_id'];
                }else{
                    $dat=$this->Decision->find('first',array('conditions'=>array('Decision.id'=>$att['Attachment']['foreign_key'])));
                    $meeting_id=$dat['Decision']['meeting_id']; 
                }
            }else{
                //status file
                if(!isset($att[$att['Attachment']['model']]['decision_id'])){
                    App::import('Model',$att['Attachment']['model']);
                    $this->{$att['Attachment']['model']}=& ClassRegistry::init($att['Attachment']['model']);
                    $this->{$att['Attachment']['model']}->recursive=-1;
                    $status=$this->{$att['Attachment']['model']}->find('first',array('conditions'=>array($att['Attachment']['model'].'.id'=>$att['Attachment']['foreign_key'])));
                    $att[$att['Attachment']['model']]=$status[$att['Attachment']['model']];
                }
                $dat=$this->Decision->find('first',array('conditions'=>array('Decision.id'=>$att[$att['Attachment']['model']]['decision_id'])));
                $meeting_id=$dat['Decision']['meeting_id'];        
            }
            //check whether the current user is secretariat of the meeting
            App::import('Model','Attendance');
            $this->Attendance=& ClassRegistry::init('Attendance');
            $attendance=$this->Attendance->find('first',array('conditions'=>array('meeting_id'=>$meeting_id,'user_id'=>$user_id)));
            if(in_array($attendance['Attendance']['attend_as'],array(1,2))){
                //if the user is chairperson or secretariat
                return 1;
            }elseif($att['Attachment']['model']=='Groupstatus'){
                App::import('Model','UsersGroup');
                $this->UsersGroup=& ClassRegistry::init('UsersGroup');
                $ingroup=$this->UsersGroup->find('first',array('conditions'=>array('group_id'=>$att['Groupstatus']['group_id'],'user_id'=>$user_id)));
                return (!empty($ingroup));
            }elseif($att['Attachment']['model']=='Userstatus'){
                return ($att['Userstatus']['user_id']==$user_id);
            }else{
                return 0;
            }
        }
    }
    
}
?>
